<?php
use yii\helpers\Html;
use yii\helpers\Url;
use app\api\widget\assets\ApiAssets;

ApiAssets::register($this);

$module = Yii::$app->controller->module->id;
$controller = Yii::$app->controller->id;
$action = Yii::$app->controller->action->id;
?>
<?php $this->beginPage() ?>
<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" charset="<?= Yii::$app->charset ?>">
    <title><?= Html::encode($this->title) ?></title>
    <?php $this->head()?>
    <style>
        .cred {
            color: red;
        }
        .title-red {
            color:#FF5722;
            font-weight: bold;
        }
        .title-argc {
            color: #009688;
        }
        .menu-box{
            width: 221px;
            position: fixed;
            top:0px;
            height: 100%;
        }
        .menu-box .search{
            height: 50px;
            padding-top: 30px;
        }
        .menu-box .menu-list{
            overflow: hidden !important;
            overflow-y: auto!important;
        }
        .layui-tree .layui-this {
            background: #5FB878;

        }
        .site-tree .layui-tree .layui-this a{
            color: #f0f0f0 !important;
        }
    </style>
</head>
<body>

<?php $this->endBody() ?>
<script>
    var api = {
        init:function () {
            this.openLayui();
            this.layout();
            this.resizeIframe();

        },
        resizeIframe:function () {
            window.addEventListener('resize',function () {
                api.layout();
            });
        },
        openLayui:function () {
            $(function () {
                api.layout();
                //我们强烈推荐你在代码最外层把需要用到的模块先加载
                layui.use(['layer', 'form', 'element'], function(){
                    var layer = layui.layer
                        ,form = layui.form
                        ,element = layui.element
                });
                //加载json
                api.jsonView();
            })
        },
        layout:function () {
            var h =  document.documentElement.clientHeight-80;
            $(".menu-list").css('height', h+'px');
            var off = $('.layui-this').offset();
            var pos = $('.layui-this').position();
            var top = parseInt(off.top-150)
            $(".menu-list").animate({scrollTop: top + "px"}, 200);
        },
        search:function (event, e) {
            if (event.keyCode===13) {
                var val = $(e).val();
                return this.load(val);
            }
        },
        load:function (key) {
            var key=key || '';
            var url = this.replaceUrl(location.href, 'key', key);

            $.get(url).done(function (res) {
                var menu = res.menu;
                var search = res.search;
                if (menu.length > 0) {
                    $('.layui-tree').html(template('tpl-menu', {menu:menu, search:search}));
                }
            });
        },
        jsonView:function () {
            $('.json-hide').each(function (i, item) {
                var str = $(item).html();
                var jsonData = eval('('+str+')');
                $(item).parent().find('.json-show').jsonViewer(jsonData);
            })
        },
        replaceUrl:function (destiny, par, par_value) {
            var pattern = par + '=([^&]*)';
            var replaceText = par + '=' + par_value;
            if (destiny.match(pattern)) {
                var tmp = '/\\' + par + '=[^&]*/';
                tmp = destiny.replace(eval(tmp), replaceText);
                return (tmp);
            } else {
                if (destiny.match('[\?]')) {
                    return destiny + '&' + replaceText;
                }
                else {
                    return destiny + '?' + replaceText;
                }
            }
            return destiny + '\n' + par + '\n' + par_value;
        }
    };
    api.init();
</script>
<div class="layui-main" style="position: relative;">
    <div class="menu-box">
        <div class="search">
            <input type="text" value="<?php echo isset($_GET['key'])?$_GET['key']:''?>" id="key" onkeyup="api.search(event, this)" placeholder="关键字查询 按下Enter搜索"  class="layui-input"/>
        </div>
        <div class="menu-list" id="menu-list">
            <div class="site-tree" style="min-height: auto">
                <ul class="layui-tree">
                    <?php if (isset($menu) && !empty($menu) && is_array($menu)):?>
                        <?php foreach ($menu as $item):?>
                            <li>
                                <h2>
                                    <?=$item['name']?>
                                    <?php if (isset($item['sign']) && (int)$item['sign']===1):?>
                                        <span class="layui-badge-dot" style="position: relative; top: -3px; left: -3px;"></span>
                                    <?php endif;?>

                                </h2>
                            </li>

                            <?php if (isset($item['items']) && !empty($item['items']) && is_array($item['items'])):?>
                                <?php foreach ($item['items'] as $v):?>

                                    <li class="site-tree-noicon <?php if ((int)$v['selected']===1):?>layui-this<?php endif;?>" >
                                        <!--layui-this-->
                                        <a <?php if (isset($search) && !empty($search)):?>href="<?=(isset(Yii::$app->params['domain']) ? '/wechat' : '').Url::to([$controller.'/'.$action , 'a'=>$v['alias'], 'key'=>$search])?>" <?php else:?>href="<?=(isset(Yii::$app->params['domain']) ? '/wechat' : '').Url::to([$controller.'/'.$action , 'a'=>$v['alias']])?>"<?php endif;?>>
                                            <cite><?=$v['title']?></cite>
                                            <?php if (isset($v['sign']) && !empty($v['sign'])):?>
                                                <span class="layui-badge" style="position: relative; top: -3px; left: -3px;"><?=$v['sign']?></span>
                                            <?php endif;?>
                                        </a>
                                    </li>
                                <?php endforeach;?>
                            <?php endif;?>


                        <?php endforeach;?>
                    <?php endif;?>
                </ul>
            </div>
        </div>
    </div>


    <div class="site-content" style="height: auto;min-height: 0;padding-left: 241px;">
        <?php if (isset($template) && !empty($template)):?>
            <?=Yii::$app->view->render("doc/{$template}", $data)?>
        <?php endif;?>

    </div>
</div>

<script id="tpl-menu" type="text/html">
    {{each menu as item}}
    <li>
        <h2>
            {{item.name}}
            {{if item.sign==1}}
            <span class="layui-badge-dot" style="position: relative; top: -3px; left: -3px;"></span>
            {{/if}}
        </h2>
    </li>
    {{if item.items}}
    {{each item.items as row}}
    <li class="site-tree-noicon {{if row.selected==1}}layui-this{{/if}}" >

        <a href="<?=Url::to([$controller.'/'.$action ])?>?a={{row.alias}}{{if search}}&key={{search}}{{/if}}">
            <cite>{{row.title}}</cite>
            {{if row.sign}}
            <span class="layui-badge" style="position: relative; top: -3px; left: -3px;">{{row.sign}}</span>
            {{/if}}
        </a>
    </li>
    {{/each}}
    {{/if}}
    {{/each}}
</script>

<?php $this->beginBody() ?>
</html>
<?php $this->endPage() ?>

